/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kasgroup2;

import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author beha
 */
public class UIConferenceOverview extends javax.swing.JPanel {

    private TableManager tm = null;
    
    /**
     * Creates new form UIConferenceOverviewJPanel
     */
    public UIConferenceOverview() {
        initComponents();
        tm = new TableManager(this.tblConferences);
        cmbState.setModel(new javax.swing.DefaultComboBoxModel(EnumConferenceState.values()));
        reloadConferenceTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblConferences = new javax.swing.JTable();
        btnDeleteConference = new javax.swing.JButton();
        btnAddConference = new javax.swing.JButton();
        cmbState = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btnOpenConferenceDetails = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Conference Overview"));

        tblConferences.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Place", "From", "To", "Speakers", "Participants", "Hotels", "Activities", "Is Active?"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblConferences.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblConferences.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConferencesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblConferences);
        tblConferences.getColumnModel().getColumn(0).setResizable(false);
        tblConferences.getColumnModel().getColumn(1).setResizable(false);
        tblConferences.getColumnModel().getColumn(2).setResizable(false);
        tblConferences.getColumnModel().getColumn(3).setResizable(false);
        tblConferences.getColumnModel().getColumn(4).setResizable(false);
        tblConferences.getColumnModel().getColumn(5).setResizable(false);
        tblConferences.getColumnModel().getColumn(6).setResizable(false);
        tblConferences.getColumnModel().getColumn(7).setResizable(false);
        tblConferences.getColumnModel().getColumn(8).setResizable(false);

        btnDeleteConference.setText("Delete");
        btnDeleteConference.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteConferenceActionPerformed(evt);
            }
        });

        btnAddConference.setText("Add");
        btnAddConference.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddConferenceActionPerformed(evt);
            }
        });

        cmbState.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Active", "Inactive" }));
        cmbState.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbStateActionPerformed(evt);
            }
        });

        jLabel1.setText("Filter by state (is active?):");

        btnOpenConferenceDetails.setText("Open");
        btnOpenConferenceDetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenConferenceDetailsActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(12, 12, 12)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(btnRefresh)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnOpenConferenceDetails)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnAddConference)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnDeleteConference)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cmbState, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 954, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnAddConference)
                    .add(btnDeleteConference)
                    .add(cmbState, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(btnOpenConferenceDetails)
                    .add(btnRefresh))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(9, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddConferenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddConferenceActionPerformed
        UICreateConferenceFrame uiccf = new UICreateConferenceFrame();
        uiccf.setAlwaysOnTop(true);
        uiccf.setVisible(true);
        uiccf.pack();
        reloadConferenceTable();
    }//GEN-LAST:event_btnAddConferenceActionPerformed

    private void btnDeleteConferenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteConferenceActionPerformed
        if(CoreHandler.ConfirmDialog(this, "Delete conference?", "Are you sure you want to delete the conference?")) {
            for(int i : this.tblConferences.getSelectedRows()) {
                //System.out.println("Deleted index: " + i);
                KASGroup2.getCoreHandlerInstance().getConferenceList().remove(i);
                tm.removeRow(i);
            }
            //reloadTable(this.tblConferences);
        }
    }//GEN-LAST:event_btnDeleteConferenceActionPerformed

    private void tblConferencesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConferencesMouseClicked
        if(evt.getClickCount() == 2 && evt.getButton() == 1) {
            openConferenceDetails();
        }
    }//GEN-LAST:event_tblConferencesMouseClicked

    private void cmbStateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbStateActionPerformed
        //System.out.println(cmbState.getSelectedItem());
        reloadConferenceTable();
    }//GEN-LAST:event_cmbStateActionPerformed

    private void btnOpenConferenceDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenConferenceDetailsActionPerformed
        openConferenceDetails();
    }//GEN-LAST:event_btnOpenConferenceDetailsActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        reloadConferenceTable();
    }//GEN-LAST:event_btnRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddConference;
    private javax.swing.JButton btnDeleteConference;
    private javax.swing.JButton btnOpenConferenceDetails;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JComboBox cmbState;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblConferences;
    // End of variables declaration//GEN-END:variables

    private void openConferenceDetails() {
        //int selRowIndex = this.tblConferences.getSelectedRows()[0];
        //int selRowId = KASGroup2.getCh().getConferenceList()
        Object selRow = tm.getCellValueForSelectedRow(0);
        if(selRow.toString().length() > 0) {
            String conferenceName = selRow.toString();//tm.getCellValue(this.tblConferences.getSelectedRows()[0], 0);
            System.out.println(conferenceName);
            UIConferenceDetailsFrame uicd = new UIConferenceDetailsFrame(conferenceName);
            uicd.setVisible(true);
            uicd.pack();
        }
        else {
            CoreHandler.MessageBox(this, "Select a conference", "Please select a conference from the table");
        }
    }
    
    private void reloadConferenceTable() {
        tm.clear();
        String selItem = this.cmbState.getSelectedItem().toString().toUpperCase();
        for(Conference c : KASGroup2.getCoreHandlerInstance().getConferenceList(selItem)) {
            tm.addRow(new Object[] { 
                c.getName(), 
                c.getPlace(), 
                c.getStartTime(), 
                c.getEndTime(), 
                c.getSpeakerList().size(),
                c.getParticipantList().size(), 
                c.getHotelList().size(), 
                c.getActivityList().size(), 
                c.getIsActive() 
            });
        }
    } 
}
